package models

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

type User struct {
	Id       int64  `json:"id";orm:"column(id);pk"`
	Username string `json:"username"`
	Password string `json:"password"`
	Email    string `json:"email"`
	Phone    string `json:"phone"`
	Icon     string `json:"icon"`
}

func UserLogin(u string, p string) (user User, code int) {
	o := orm.NewOrm()
	fmt.Println("查询", u)
	params := User{Username: u, Password: p}
	read := o.Read(&params, "username", "password")
	fmt.Println(read)
	if read == nil {
		//查询成功,已存在
		return params, 0
	}
	return user, -1
}

func UserRegister(u string, p string) (user User, code int) {
	o := orm.NewOrm()
	fmt.Println("查询", u)
	params := User{Username: u}
	read := o.Read(&params, "username")
	fmt.Println(read)
	if read == nil {

		//查询成功,已存在
		return params, SQL_EXSIT
	} else {
		params = User{Username: u, Password: p}
		id, e := o.Insert(&params)
		fmt.Println(id, e)
		if id > -1 {
			params.Id = id
			return params, SQL_SUCCESS
		}
	}
	fmt.Println("readresult: ", read)
	return user, SQL_UNKNOW
}
